package cn.xlbweb.cli.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.xlbweb.cli.model.Permission;
import cn.xlbweb.cli.model.User;
import cn.xlbweb.cli.pojo.vo.ListPermissionVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: bobi
 * @date: 2018/11/18 20:53
 * @description: 系统工具类
 */
public class CliUtils {

    /**
     * 复制非空属性
     *
     * @param source
     * @param target
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
    }

    /**
     * 获取本机IP地址
     *
     * @return
     */
    public static String getLocalIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "127.0.0.1";
    }

    /**
     * 获取当前用户用户ID
     *
     * @return
     */
    public static Integer getCurrentUserId() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return user.getId();
    }

    /**
     * List转换为Set
     *
     * @param list
     * @return
     */
    public static Set<String> listToSet(List<String> list) {
        Set<String> set = new HashSet<>();
        for (String str : list) {
            if (StringUtils.isBlank(str)) {
                continue;
            }
            set.addAll(Arrays.asList(str.trim().split(",")));
        }
        return set;
    }

    /**
     * 获取批量操作的id集合
     *
     * @param ids
     * @return
     */
    public static List<Integer> getIdList(String ids) {
        String[] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i < idArray.length; i++) {
            idList.add(Integer.valueOf(idArray[i]));
        }
        return idList;
    }

    /**
     * String字符串转换为String List集合
     *
     * @param str
     * @return
     */
    public static List<String> strToStringList(String str) {
        return Stream.of(str.split(",")).collect(Collectors.toList());
    }

//    public static List<String> strToStringList(String str) {
//        String[] excludeUriArray = str.split(",");
//        List<String> excludeUriList = new ArrayList<>();
//        for (String excludeUri : excludeUriArray) {
//            excludeUriList.add(excludeUri);
//        }
//        return excludeUriList;
//    }

    /**
     * String字符串转换为Integer List集合
     *
     * @param str
     * @return
     */
    public static List<Integer> strToIntegerList(String str) {
        return Stream.of(str.split(",")).map(item -> Integer.parseInt(item.trim())).collect(Collectors.toList());
    }

    /**
     * 递归获取权限树
     *
     * @param permissionList
     * @param parentId
     * @return
     */
    public static List<ListPermissionVO> getPermissionTree(List<Permission> permissionList, Integer parentId) {
        List<ListPermissionVO> listPermissionsVOList = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getPid().equals(parentId)) {
                ListPermissionVO listPermissionVo = new ListPermissionVO();
                CliUtils.copyProperties(permission, listPermissionVo);
                listPermissionsVOList.add(listPermissionVo);
            }
        }
        for (ListPermissionVO listPermissionVo : listPermissionsVOList) {
            listPermissionVo.setChildren(getPermissionTree(permissionList, listPermissionVo.getId()));
        }
        return listPermissionsVOList;
    }
}
